using Godot;
using System;
using System.Collections.Generic;
namespace DRE.TextWriter {
	public class TextInstance : Node2D {
		delegate void EffectDelegate(TextInstance inst);
		static Dictionary<string, EffectDelegate> effects = new Dictionary<string, EffectDelegate>(){
			{
				"Shake",(inst)=>{
					inst.Position = inst.OriginalPosition + new Vector2(Manager.Random.Next(-1, 2), Manager.Random.Next(-1, 2));
				}
			},
			{
				"Wavy",(inst)=>{

				}
			}
		};

		string text;
		public string Text {
			get => text;
			set {
				text = value;
				LabelText.Text = text;
				LabelShadow.Text = text;
			}
		}
		public Label LabelText { get; private set; }
		public Label LabelShadow { get; private set; }
		bool shadow;
		public bool Shadow {
			get => shadow;
			set {
				shadow = value;
				LabelShadow.Visible = shadow;
			}
		}
		public string Effect { get; set; }
		string font { get; set; }
		public string Font {
			get => font;
			set {
				font = value;
				Settings.FontResource fr = Settings.GetFontSet(font).GetFontResource(text[0]);
				Godot.Font ft = GD.Load<Font>(fr.Path);
				LabelText.AddFontOverride("font", ft);
				LabelShadow.AddFontOverride("font", ft);
			}
		}
		string color { get; set; }
		public string Color {
			get => color;
			set {
				color = value;
				Settings.ColorSet cs = Settings.GetColorSet(color);
				LabelText.Modulate = cs.Text.Up;
				LabelShadow.Modulate = cs.Shadow.Up;
			}
		}
		Vector2 originalScale;
		public Vector2 OriginalScale {
			get => originalScale;
			set {
				originalScale = value;
				Scale = originalScale;
				LabelShadow.RectPosition = 0.5f * originalScale;
			}
		}
		Vector2 originalPosition;
		public Vector2 OriginalPosition {
			get => originalPosition;
			set {
				originalPosition = value;
				Position = value;
			}
		}
		public float tick = 0;
		public override void _Ready() {
			LabelText = GetNode<Label>("Text");
			LabelShadow = GetNode<Label>("Shadow");
		}
		public override void _Process(float delta) {
			if (Effect != "None") {
				tick += delta;
				if (tick >= 1f / 30) {
					OnEffect();
					tick -= 1f / 30;
				}
			}
		}
		void OnEffect() {
			if(!effects.ContainsKey(Effect)){
				return;
			}
			effects[Effect](this);
		}
	}
}
